package com.hjm.app.activity;

import android.os.Bundle;
import android.os.Handler;
import android.util.ArrayMap;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.hjm.app.R;
import com.hjm.java.service.AddressService;
import com.hjm.java.service.CommentService;
import com.hjm.java.service.GoodsService;
import com.hjm.java.service.MainService;
import com.hjm.java.service.OrderService;
import com.hjm.java.service.StoreService;
import com.hjm.java.service.UserService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 基础Activity，所有的Activity都应该继承自这个Activity
 * 如果需要使用Fragment，FrameLayout的id必须是frame_layout
 */
public abstract class BaseActivity extends AppCompatActivity{
    Handler mHandler;

    private static final String TAG = "BaseActivity";

    List<Fragment> mFragmentList;
    Map<String,Fragment> mFragmentMap;

    // 在类中定义一个变量来存储前一个片段
    private Fragment mPreviousFragment;

    int mPrevIndex = 0;

    UserService userService;
    MainService mainService;
    GoodsService goodsService;
    StoreService storeService;
    OrderService orderService;
    AddressService addressService;
    CommentService commentService;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getView());
        mHandler = new Handler(getMainLooper());
        userService = new UserService(this, mHandler);
        mainService = new MainService(this, mHandler);
        goodsService = new GoodsService(this, mHandler);
        storeService = new StoreService(this, mHandler);
        orderService = new OrderService(this, mHandler);
        addressService = new AddressService(this,mHandler);
        commentService = new CommentService(this,mHandler);

        mFragmentList = new ArrayList<>();
        mFragmentMap = new ArrayMap<>();

        initView();
        initData();
    }

     void switchFragmentPosition(int position) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment currentFragment = mFragmentList.get(position);
        Fragment prevFragment = mFragmentList.get(mPrevIndex);
        mPrevIndex = position;
        ft.hide(prevFragment);
        if (!currentFragment.isAdded()) {
            ft.add(R.id.frame_layout, currentFragment);
        }
        ft.show(currentFragment);
        ft.commitAllowingStateLoss();
    }
    void switchFragmentPosition(String type) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment currentFragment = mFragmentMap.get(type);
        if (currentFragment == null) {
            Log.d(TAG, "switchFragmentPosition: 片段不存在: " + type);
            return;
        }

        if (mPreviousFragment != null) {
            ft.hide(mPreviousFragment);
        }

        if (!currentFragment.isAdded()) {
            ft.add(R.id.frame_layout, currentFragment);
        }

        ft.show(currentFragment);
        ft.commitAllowingStateLoss();

        // 更新 mPreviousFragment 变量
        mPreviousFragment = currentFragment;
    }

    /**
     * 初始化视图
     */
    abstract void initView();

    abstract void initData();

    abstract int getView();
}
